package com.bestcem.xm.award.grpc.client;

import com.bestcem.bp.pay.grpc.v1.common.CreateResponse;
import com.bestcem.bp.pay.grpc.v1.services.*;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.sign.grpc.core.GrpcTemplate;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 钱包服务接口
 *
 * @author sh.pei <sh.pei@idiaoyan.com>
 * @version v1.0
 * @date 2021/7/14 19:31
 */
@Service
@Slf4j
public class WalletGrpcService {

    @GrpcClient(GrpcClientNames.PAY)
    private WalletServiceGrpc.WalletServiceBlockingStub walletServiceBlockingStub;

    @Resource
    private GrpcTemplate grpcTemplate;

    /**
     * 获取钱包
     *
     * @param request 获取钱包请求参数
     * @return 获取钱包结果
     */
    public GetWalletResponse getWallet(GetWalletRequest request) {
        try {
            grpcTemplate.addSign(request, GrpcTemplate.SEVER_CODE_PAY);
            return walletServiceBlockingStub.getWallet(request);
        } catch (Exception e) {
            log.error("[award] pay getWallet {} Error; ErrorMsg: {};", request, e.getMessage(), e);
            return GetWalletResponse.newBuilder().setStatus(RpcStatusResult.error("钱包服务调用失败")).build();
        }
    }

    /**
     * 创建钱包
     *
     * @param request 创建钱包入参
     * @return 创建钱包结果
     */
    public CreateResponse createPlatformWallet(CreatePlatformWalletRequest request) {
        try {
            grpcTemplate.addSign(request, GrpcTemplate.SEVER_CODE_PAY);
            return walletServiceBlockingStub.createPlatformWallet(request);
        } catch (Exception e) {
            log.error("[award] pay createPlatformWallet {} Error; ErrorMsg: {};", request, e.getMessage(), e);
            return CreateResponse.newBuilder().setStatus(RpcStatusResult.error("钱包服务调用失败")).build();
        }
    }

    /**
     * 获取钱包占用金额
     *
     * @param request 获取钱包占用金额请求参数
     * @return 获取钱包占用金额结果
     */
    public GetWalletFreezeResponse getWalletFreeze(GetWalletFreezeRequest request) {
        try {
            grpcTemplate.addSign(request, GrpcTemplate.SEVER_CODE_PAY);
            return walletServiceBlockingStub.getWalletFreeze(request);
        } catch (Exception e) {
            log.error("[award] pay getWalletFreeze {} Error; ErrorMsg: {};", request, e.getMessage(), e);
            return GetWalletFreezeResponse.newBuilder().setStatus(RpcStatusResult.error("钱包服务调用失败")).build();
        }
    }

    /**
     * 余额充值
     *
     * @param request
     * @return
     */
    public RechargeWalletResponse rechargeWallet(RechargeWalletRequest request) {
        try {
            grpcTemplate.addSign(request, GrpcTemplate.SEVER_CODE_PAY);
            return walletServiceBlockingStub.rechargeWallet(request);
        } catch (Exception e) {
            log.error("[award] pay rechargeWallet {} Error; ErrorMsg: {};", request, e.getMessage(), e);
            return RechargeWalletResponse.newBuilder().setStatus(RpcStatusResult.error("钱包服务调用失败")).build();
        }
    }
}
